/**
 * 
 */
package com.vit.mep;

import java.io.IOException;

import net.rim.device.api.crypto.CryptoException;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import com.vit.bb.VitScreen;
import com.vit.mep.manager.TransactionManager;
import com.vit.mep.model.BankResponse;
import com.vit.mep.model.TransactionData;

/**
 * @author crodriguez
 *
 */
public class ConfirmationScreen extends VitScreen {

	
	protected MepApp _mepApp;
	private  TransactionData _transactionData;
	

	protected LabelField _lblMonto;
	protected LabelField _lblNombre;	
	protected LabelField _lblEmail;
	protected LabelField _lblCardNumber;
	protected LabelField _lblExpDate;
	protected LabelField _lblCvv;
	
	protected LabelField _lblStatus;
	
	protected ButtonField _btnProcess;
	protected ButtonField _btnCancel;
	
	public ConfirmationScreen(MepApp mepApp){
		_mepApp = mepApp;
		init();
	}
	
	
	private void init(){
		setTitle("Confirmacion");
		
		_lblMonto = new LabelField("");
		_lblNombre = new LabelField("");
		_lblEmail  = new LabelField("");		
		_lblCardNumber  =new LabelField("");
		_lblExpDate = new LabelField("");
		_lblCvv = new LabelField("");
		
		_lblStatus = new LabelField("");
		
		
		_vfManager.add(_lblMonto);
		_vfManager.add(_lblNombre);
		_vfManager.add(_lblCardNumber);
		_vfManager.add(_lblExpDate);
		_vfManager.add(_lblCvv);
		
		_btnProcess = new ButtonField("Procesar", ButtonField.CONSUME_CLICK);
		_btnCancel = new ButtonField("Cancelar", ButtonField.CONSUME_CLICK);
		
		_btnProcess.setChangeListener(new ProcessListener());
		_btnCancel.setChangeListener(new CancelListener());
		
		_hfManager.add(_btnProcess);
		_hfManager.add(_btnCancel);
		
		add(_vfManager);
		add(_hfManager);
		setStatus(_lblStatus);
	} //init
	
	
	public void repaint(TransactionData tdata){
		_lblMonto.setText("Monto: "+ tdata.getMonto());
		_lblNombre.setText("Nombre: " + tdata.getNombre());
		_lblEmail.setText("Email:" + tdata.getEmail());
		_lblExpDate.setText("Exp: " + tdata.getExpDate());
		_lblCvv.setText("CVV: " + "***" + tdata.getCvvLbl());
		_lblCardNumber.setText("Tarjeta: " + "************"+tdata.getCardNumberLbl());	
		setTransactionData(tdata);		
		//_lblStatus.setText(tdata.getCardData());
	}
	
	public void clearForm(){
		_lblMonto.setText("");
		_lblNombre.setText("");
		_lblEmail.setText("");
		_lblExpDate.setText("");
		_lblCardNumber.setText("");
		_lblStatus.setText("");
	}
	
	
	
	class CancelListener implements FieldChangeListener{		
		public void fieldChanged(Field field, int context) {	
			clearForm();
			_mepApp.showReaderScreenFromConfirmation();
		}		
	}//CancelListener
	
	class ProcessListener implements FieldChangeListener{		
		public void fieldChanged(Field field, int context) {
			//TODO:enviar transaccion final
			_lblStatus.setText("");
			TransactionManager tmanager = new TransactionManager();
			try {
				_lblStatus.setText("Procesando transaccion...");
				BankResponse br = tmanager.authTransaction(_mepApp.getMepUser(), _transactionData);
				_lblStatus.setText("Completando...");
				_lblStatus.setText("");
				_mepApp.showVoucher(br);				
			} catch (IOException e) {
				_lblStatus.setText("error:" + e.getMessage());
			}catch (MepException e) {
				_lblStatus.setText("error:"+ e.getAditionalMessage());
			} catch (CryptoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}//CancelListener

	

	public void setTransactionData(TransactionData transactionData) {
		this._transactionData = transactionData;
	}
	
	
}//class